﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script type="text/javascript" src="../../Content/highlighter/scripts/shCore.js"></script>
    <script type="text/javascript" src="../../Content/highlighter/scripts/shBrushJScript.js"></script>
    <script type="text/javascript" src="../../Content/highlighter/scripts/shBrushCSharp.js"></script>
    <script type="text/javascript" src="../../Content/highlighter/scripts/shBrushXml.js"></script>
    <link type="text/css" rel="stylesheet" href="../../Content/highlighter/styles/shCore.css" />
    <link type="text/css" rel="stylesheet" href="../../Content/highlighter/styles/shCoreEclipse.css" />
    <script type="text/javascript">SyntaxHighlighter.all();</script>
</head>
<body>
    <h3>添加简单的视图</h3>
    1.把IndexController改为如下代码：
    <pre class="brush:csharp;">
        public class IndexController : BaseController
        {
            public void Index()
            {
                this.ViewBag.Title="首页";
                this.ViewBag.Message="HelloWorld!";
                //相当于this.Render("/SimpleWebApp/Views/Index/Index.cshtml");
                this.Render();
            }
        }
    </pre>
    保存后会生成IndexController.model.cs子文件，代码如下：
    <pre class="brush:csharp;">
    namespace SimpleWebApp.Controllers.IndexController_Model
    {
        public class Default
	    {
		    [NFinal.ViewBagMember]
		    public NFinal.Config.Plug.PlugConfig config;
		    public string Message;
		    public string Title;
	    }
    }
    </pre>
    2.在项目根目录下新建/Views/Index/目录，并新建Index.cshtml文件
    文件内容如下：<br/>
    NFinal.Views.RazorView的泛型参数为控制器自动生成的实体类<br/>
    <pre class="brush:js;html-script:true;">
    @inherits NFinal.Views.RazorView&lt;SimpleWebApp.Controllers.IndexController_Model.Index>
    &lt;!DOCTYPE html>
    &lt;html>
    &lt;head>
    &lt;title>@Model.Title&lt;/title>
    &lt;/head>
    &lt;body>
    //在该视图中可以使用Razor的基础语法，@,if,while,for,foreach等。
    @Model.Message
    &lt;/body>
    &lt;/html>
    </pre>
    保存后会生成Index.template.cs子文件，代码如下：
    <pre class="brush:csharp;">
    using NFinal;

    //此代码由NFinalRazorGenerator生成。
    //http://bbs.nfinal.com
    namespace NFinalCorePlug.Views.Index
    {
        [View("/SimpleWebApp/Views/Index/Default.cshtml")]
        public class Default : NFinal.View.RazorView&lt;SimpleWebApp.Controllers.IndexController_Model.Default>
        {
            public Default(NFinal.IO.Writer writer, SimpleWebApp.Controllers.IndexController_Model.Default Model) : base(writer, Model)
            {
            }

            /// <summary>
            /// 输出模板内容
            /// </summary>
            public override void Execute()
            {
                this.ExecuteAsync().Wait();
            }
            //// <summary>
            /// 输出模板内容(异步)
            /// </summary>
            public override async Task ExecuteAsync()
            {
                await writer.WriteAsync("");
                await writer.WriteAsync("&lt;!DOCTYPE html>\r\n&lt;html>\r\n\r\n&lt;head>&lt;title>");
                await writer.WriteAsync(Model.Title);
                await writer.WriteAsync("&lt;/title>\r\n&lt;body>\r\n");
                await writer.WriteAsync(Model.Message);
                await writer.WriteAsync("&lt;/body>\r\n&lt;/html>");
            }
        }
    }
    </pre>
    3.启动该项目，在项目根目录下会自动生成Debug/SimpleWebApp/Index/Index.html,<br/>
    打开该页面，页面将显示HelloWorld!
</body>
</html>